package com.fy.utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@Component
public class DateUtil {
	
	public static final String FORMAT_YMD_DEFAULT = "yyyy-MM-dd";

	public static final String FORMAT_YMD = "yyyyMMdd";

	public static final String FORMAT_DAY_TIME = "yyyy-MM-dd HH:mm:ss";

    // date类型转换为String类型
    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    // long类型转换为String类型
    // currentTime要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    public static String longToString(long currentTime, String formatType)
            throws ParseException {
        Date date = longToDate(currentTime, formatType); // long类型转成Date类型
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }

    // string类型转换为date类型
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    // string类型转换为long类型
    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // date类型转换为long类型
    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    //string时间转秒 时分秒
    public  static long Stringtime(String time){
        if (time.length() > 5){
            String[] videotime = time.split(":");
            long hour = Long.parseLong(videotime[0]);
            long min = Long.parseLong(videotime[1]);
            long sec = Long.parseLong(videotime[2]);
            long s = hour*3600+min*60+sec;
            return  s;
        }else {
            String[] videotime = time.split(":");
            long min = Long.parseLong(videotime[0]);
            long sec = Long.parseLong(videotime[1]);
            long s = min*60+sec;
            return  s;
        }

    }
    //string时间转秒 分秒
    public  static long StringMintime(String time){
        String[] videotime = time.split(":");
        long min = Long.parseLong(videotime[0]);
        long sec = Long.parseLong(videotime[1]);
        long s = min*60+sec;
        return  s;
    }
    //秒转string时间
    public  static String longtime(long time){
        long hours = time/ ( 60 * 60);
        long minutes = (time-hours*(60 * 60 ))/60;
        long secs = (time-hours*(60 * 60 )-minutes*60);
        String diffTime="";
        if(minutes<10){
            if (secs < 10){
                diffTime=hours+":0"+minutes+":0"+secs;
            }else {
                diffTime=hours+":0"+minutes+":"+secs;
            }
        }else{
            if (secs < 10){
                diffTime=hours+":"+minutes+":0"+secs;
            }else {
                diffTime=hours+":"+minutes+":"+secs;
            }
        }
        return  diffTime;
    }

    /**
     *秒转时分秒
     * @param time 以秒为单位
     * @return
     */
    public  static String longTimeCH(long time){
        long hours = time/ ( 60 * 60);
        long minutes = (time-hours*(60 * 60 ))/60;
        long secs = (time-hours*(60 * 60 )-minutes*60);
        String diffTime="";

        if(hours > 0){
            diffTime += hours+"小时";
        }
        if(minutes > 0){
            if(minutes<10){
                if (secs < 10){
                    diffTime += minutes+"分0"+secs + "秒";
                }else {
                    diffTime += minutes+"分"+secs + "秒";
                }
            }else{
                if (secs < 10){
                    diffTime += minutes+"分0"+secs + "秒";
                }else {
                    diffTime += minutes+"分"+secs + "秒";
                }
            }
        }
        return  diffTime;
    }

    public static long testBetweenDays(String dateStr1,String dateStr2) throws ParseException {
        // 获取日期
        Date date1 = stringToDate(dateStr1, "yyyy-MM-dd");
        Date date2 = stringToDate(dateStr2, "yyyy-MM-dd");
        // 获取相差的天数
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        long timeInMillis1 = calendar.getTimeInMillis();
        calendar.setTime(date2);
        long timeInMillis2 = calendar.getTimeInMillis();

        long betweenDays =  (timeInMillis2 - timeInMillis1) / (1000L*3600L*24L);
        return betweenDays;
    }
}
